<template>
  <div class="wrapper" v-if="detail.id">
    <div class="goods-list">
      <p class="title">有惊喜</p>
      <GoodsItem
        :title="detail.name"
        :price="detail.subtotal"
        :count="Number(detail.order_num)"
        :img="detail.url"
        :key="detail.id"
      ></GoodsItem>
    </div>
    <div class="item">
      <p>退货原因</p>
      <textarea maxlength="100" placeholder="填写原因,不超过100字" v-model="reason"></textarea>
    </div>
    <div class="item">
      <p>退货数量</p>
      <input type="tel" v-model="num" placeholder="请输入退货数量">
    </div>
    <div class="item">
      <p>上传图片(最多三张)</p>
      <div class="img-wrapper">
        <div class="img" v-for="item of imgArr" :key="item.id">
          <img :src="item.url" preview>
          <i @click="del(item.id)">X</i>
        </div>
        <label for="file" class="upload" v-show="imgArr.length < 3">
          <label for="file">点击上传</label>
        </label>
      </div>
    </div>
    <div class="bar">
      <button class="submit" @click="returnSubmit">提交</button>
    </div>
    <input type="file" id="file" ref="file" @change="change">
  </div>
</template>

<script>
  import _ from 'lodash'
  import GoodsItem from '@/components/goodsItem/GoodsItem.vue'
  import { mapState } from 'vuex'
  export default {
    name: 'OrderDetailReturn',
    components: {
      GoodsItem
    },
    created() {
      this._get()
    },
    data() {
      return {
        detail: {},
        reason: '',
        num: '',
        imgArr: [],
        logisArr: [],
        show: false
      }
    },
    computed: {
      ...mapState(['openId', 'userState']),
      apiUrl() {
        return this.userState == 0 ? '/v1/merchant/show' : '/v1/platform/show'
      },
      imgArrId() {
        let arr = this.imgArr.map(item => {
          return item.id
        })
        return arr.toString()
      }
    },
    methods: {
      async change() {
        const img = this.$refs.file.files[0]
        const type = img.type.split('/')[1]
        this.$refs.file.value = ''
        if(type != "jpeg" && type != "png" && type != "jpg") {
          this.$toast('请上传jpeg,png,jpg格式图片')
          return
        }
        if(img.size / 1024 / 1024 > 5) {
          this.$toast('图片不能大于5MB')
          return
        }
        let formData = new FormData()
        formData.append('name', img)
        let { data: { code, data } } = await this.$axios({
          url: '/v1/merchant/image',
          method: 'POST',
          data: formData
        })
        if(code === 0) {
          this.imgArr.push({
            id: data.id,
            url: data.url
          })
          this.$nextTick(() => {
            this.$previewRefresh()
          })
        }
      },
      del(id) {
        this.imgArr = this.imgArr.filter(item => {
          return item.id !== id
        })
      },
      async _get() {
        let { data: { code, data } } = await this.$axios({
          url: this.apiUrl,
          method: 'GET',
          params: {
            order_code: this.$route.params.order_code,
            openid: this.openId
          }
        })
        if(code === 0) {
          this.detail = data[this.$route.params.index]
        }
      },
      returnSubmit() {
        if(!this.reason) {
          this.$toast('请输入退货原因')
          return
        }
        if(!this.num) {
          this.$toast('请输入退货数量')
          return
        }
        if(isNaN(this.num)) {
          this.$toast('请正确输入退货数量')
          return
        }
        if(this.num > this.detail.order_num) {
          this.$toast('退货数量不能大于购买数量')
          return
        }
        if(this.imgArr.length == 0) {
          this.$toast('请上传图片')
          return
        }
        this._returnSubmit()
      },
      _returnSubmit: _.throttle(async function() {
        let { data: { code, data  } } = await this.$axios({
          url: '/v1/merchant/apply',
          method: 'POST',
          data: {
            order_code: this.$route.params.order_code,
            pic_id: this.imgArrId,
            reason: this.reason,
            id: this.detail.id,
            out_num: this.num,
            pice: this.detail.subtotal,
            openid: this.openId
          }
        })
        if (code === 0) {
          this.$toast(data)
          setTimeout(() => {
            this.$router.replace({
              name: 'Center'
            })
          }, 1500)
        }
      }, 2000, { trailing: false })
    }
  }
</script>

<style lang="scss" scoped>
  @import '@/assets/css/base.scss';
  #file {
    display: none;
  }
  .wrapper {
    min-height: 100vh;
    background: #eee;
    padding-top: 0.2rem;
    .top {
      text-align: center;
      margin-bottom: 0.2rem;
      padding: 0.6rem 0;
      background: orange;
      color: #fff;
      h3 {
        font-size: 0.32rem;
      }
      h5 {
        font-size: 0.28rem;
      }
    }
    .item {
      display: flex;
      padding: 0.2rem;
      background: #fff;
      font-size: 0.28rem;
      p {
        padding: 0.06rem;
        flex: 0 0 1.5rem;
        width: 1.5rem;
      }
      textarea {
        flex: 1;
        display: block;
        height: 1.6rem;
        resize: none;
        background: #eee;
        font-size: 0.28rem;
        padding: 0.1rem;
      }
      span {
        display: block;
        flex: 0 0 2rem;
        width: 2rem;
        margin-right: 0.4rem;
      }
      input {
        flex: 1;
        display: block;
        font-size: 0.24rem;
      }
      select {
        flex: 1;
        display: block;
        font-size: 0.24rem;
        padding: 0;
      }
      .img-wrapper {
        flex: 1;
        .upload {
          border: 1px dashed $colorGray;
          margin-right: 0;
          border-radius: 5px;
          text-align: center;
          z-index: 3;
          label {
            position: absolute;
            @include center;
            z-index: -1;
          }
        }
        .img {
          margin-right: 0.2rem;
        }
        .img, .upload {
          position: relative;
          display: inline-block;
          width: 1.5rem;
          height: 1.5rem;
          vertical-align: middle;
          img {
            width: 100%;
            height: 100%;
          }
          i {
            position: absolute;
            font-style: normal;
            font-size: 0.2rem;
            right: 0;
            top: 0;
            transform: translate3d(50%, -50%, 0);
            width: 0.3rem;
            height: 0.3rem;
            line-height: 0.3rem;
            background: rgba(0, 0, 0, 0.6);
            color: #fff;
            text-align: center;
            border-radius: 50%;
          }
        }
      }
    }
    .bar {
      margin-top: 0.2rem;
      // background: #fff;
      padding: 0.2rem;
      text-align: center;
      button {
        padding: 0.2rem;
        border:none;
        border-radius: 5px;
        color: #fff;
        font-size: 0.28rem;
      }
      .submit {
        display: block;
        margin: 0 auto;
        width: 80%;
        background: $colorRed;
      }
      .confirm {
        margin: 0 0.2rem;
        display: inline-block;
        width: 2.5rem;
        &.reject {
          background: orange;
        }
        &.resolve {
          background: $colorRed;
        }
      }
    }
    .goods-list {
      padding: 0 0.2rem;
      margin-bottom: 0.2rem;
      background: #FFF;
      .title {
        padding: 0.1rem 0;
        font-size: 0.24rem;
        border-bottom: 1px solid $bgGray;
      }
    }
  }
</style>